package com.qyhl.webtv.module_user.util;

import android.content.Context;
import androidx.core.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class NestedScrollViewForRecyclerview extends NestedScrollView {
    private float nowY;

    public NestedScrollViewForRecyclerview(Context context) {
        super(context);
    }

    public NestedScrollViewForRecyclerview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NestedScrollViewForRecyclerview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                nowY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (ev.getY() < nowY) {
                    int dis = (int) (nowY - ev.getY());
                    nowY = ev.getY();
                    //上滑
                    View childView = getChildAt(0);
                    if (childView != null && childView.getMeasuredHeight() > (getScrollY() + getHeight())) {
                        smoothScrollBy(0, dis);
                        return true;
                    }
                }
                break;
            default:
        }
        return super.dispatchTouchEvent(ev);
    }

}
